package cn.tedu.hotel.service.impl;

import cn.tedu.hotel.ex.ServiceException;
import cn.tedu.hotel.mapper.RoomMapper;
import cn.tedu.hotel.mapper.RoomTypeMapper;
import cn.tedu.hotel.mapper.CategoryMapper;
import cn.tedu.hotel.pojo.dto.RoomTypeAddNewDTO;
import cn.tedu.hotel.pojo.entity.Category;
import cn.tedu.hotel.pojo.entity.RoomType;
import cn.tedu.hotel.pojo.vo.RoomStandardVO;
import cn.tedu.hotel.pojo.vo.RoomTypeStandardVO;
import cn.tedu.hotel.pojo.vo.CategoryStandardVO;
import cn.tedu.hotel.service.IRoomTypeService;
import cn.tedu.hotel.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class RoomTypeServiceImpl implements IRoomTypeService, ServiceCode {

    @Autowired
    RoomTypeMapper roomTypeMapper;
    @Autowired
    RoomMapper roomMapper;
    @Autowired
    CategoryMapper categoryMapper;

    public RoomTypeServiceImpl(){
        log.debug("创建业务对象：RoomTypeServiceImpl");
    }

    @Override
    public void addNew(RoomTypeAddNewDTO roomTypeAddNewDTO) {
        log.debug("开始处理【添加房间与房间类型绑定关系】的业务，参数：{}",roomTypeAddNewDTO);
        Long roomId = roomTypeAddNewDTO.getRoomId();
        Long typeId = roomTypeAddNewDTO.getTypeId();

        //检查房间数据是否存在
        {
            RoomStandardVO queryResult = roomMapper.getStandardById(roomId);
            if (queryResult == null){
                String message = "添加房间与房间类型绑定失败，房间数据不存在";
                log.warn(message);
                throw new ServiceException(ERR_NOT_FOUND,message);
            }
        }

        //检查房间类型数据是否存在
        {
            CategoryStandardVO queryResult = categoryMapper.getStandardById(typeId);
            if (queryResult == null){
                String message = "添加房间与房间类型绑定失败，房间类型数据不存在";
                log.warn(message);
                throw new ServiceException(ERR_NOT_FOUND,message);
            }
        }

        //检查房间与房间类型是否存在关联
        {
            int count = roomTypeMapper.countByRoomAndType(roomId, typeId);
            if (count > 0) {
                String message = "添加房间与房间类型的绑定失败！此房间与房间类别的绑定已存在！";
                log.warn(message);
                throw new ServiceException(ERR_CONFLICT, message);
            }
        }

        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        //创建实体对象
        RoomType roomType = new RoomType();
        //补全实体对象的属性值
        BeanUtils.copyProperties(roomTypeAddNewDTO,roomType);
        roomType.setGmtCreate(now);
        roomType.setGmtModified(now);
        //执行插入数据
        int rows = roomTypeMapper.insert(roomType);
        if (rows != 1) {
            String message = "添加房间与房间类型的绑定失败！服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }

    }



    @Override
    public void deleteByRoomId(Long roomId) {
        log.debug("开始处理【删除roomId={}的房间与类型关联数据】的业务",roomId);
        //检查此房间与类型关联数据是否存在
        RoomTypeStandardVO queryResult = roomTypeMapper.getStandardRoomTypeById(roomId);
        if (queryResult == null){
            String message = "删除房间与房间类型关联失败，访问数据不存在";
            log.warn(message);
            throw new ServiceException(ERR_NOT_FOUND,message);
        }

        //执行删除
        int rows = roomTypeMapper.deleteByRoomId(roomId);
        if (rows != 1){
            String message = "删除失败，服务器繁忙，请稍后再试";
            log.warn(message);
            throw new ServiceException(ERR_DELETE,message);
        }

        // 修改房间数量
        {
            Long typeId = queryResult.getTypeId();

            CategoryStandardVO standardById = categoryMapper.getStandardById(typeId);
            if (standardById == null){
                String message = "房间类型不存在，无法修改";
                log.debug(message);
                throw new ServiceException(ERR_NOT_FOUND, message);
            }

            Integer total = standardById.getTotal()-1;
            Integer surplus = standardById.getSurplus()-1;
            Category category = new Category();
            category.setId(typeId);
            category.setTotal(total);
            category.setSurplus(surplus);
            categoryMapper.update(category);
        }

    }

}
